<?php
class Tourismprovider extends CS_Controller
{
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('tourism_provider_model', 'tourism_provider');
        $this->load->model('tourism_goods_category_model','tourism_goods_category');
        $this->load->model('tourism_goods_base_model', 'tourism_goods_base');
        $this->load->model('tourism_provider_category_model','tourism_provider_category');
        $this->load->model('region_model','region');
        $this->load->model('operate_power_log_model', 'operate_power_log');
    }
    
    public function grid($pg = 1)
    {
        $page_num = 20;
        $num = ($pg - 1) * $page_num;
        $config['first_url'] = base_url('tourismprovider/grid').$this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('tourismprovider/grid');
        $config['total_rows'] = $this->tourism_provider->total($this->input->get());
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_list'] = $this->pagination->create_links();
        $data['page_list'] = $this->tourism_provider->page_list($page_num, $num, $this->input->get());
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['url'] = $this->pageGetParam($this->input->get());
        $this->load->view('tourismprovider/grid', $data);
    }
    
    public function add()
    {
        $data['category'] = $this->tourism_goods_category->getCategoryByReason($level=1,$parent_id=0);
        $this->load->view('tourismprovider/add',$data);
    }
    
     /**
     * 批量上下架
     */
    public function batchFlag()
    {
        $flag = (int)$this->input->post('type');
        $pg = (int)$this->input->post('pg');
        $providerIdArray = $this->input->post('id');
        if (count($providerIdArray) <=0) {
            $this->jsonMessage('请选择商品');
        }
        if (empty($flag)) {
            $this->jsonMessage('请选择操作类型');
        }
        $result = $this->tourism_provider->updateTourismProviderFlag($flag, $providerIdArray);
        if($result) {
            $this->session->set_flashdata('success', '操作成功!');
            $this->jsonMessage('',site_url('tourismprovider/grid/'.$pg));
        }
        $this->session->set_flashdata('error', '操作失败!');
        $this->jsonMessage('操作失败');
    }
    
    public function addPost()
    {
        $postData = $this->input->post();
        $error = $this->validate($postData); 
        $result = $this->tourism_provider->getProviderByUid($postData['uid']);
        if (!empty($error)) {
            $this->error('tourismprovider/grid', '', $error);
        }
        if ( $result->num_rows() > 0 ){
            $this->error('tourismprovider/grid', '', '此供应商材料已经上传');
        }
        $imageData = array(
           'logoimage' => $postData['logo'],
           'wapimage'  => $postData['wap_image']
        );
        $this->db->trans_start();
        $this->tourism_provider->insertTourismProvider($postData, $imageData);
        $this->db->trans_complete();
        if ($this->db->trans_status === false) {
            $this->error('tourismprovider/add', '', '上传失败！');
        } else {
            $this->success('tourismprovider/grid', '', '上传成功');
        }
    }
    
    public function edit($id)
    {
        $result = $this->tourism_provider->findById($id);
        if ($result->num_rows() <= 0) {
            $this->success('tourismprovider/grid', '', '没有该序列值');
        }
        $data['category'] = $this->tourism_goods_category->getCategoryByReason($level=1,$parent_id=0);
        $data['provider'] = $result->row(0);
        $this->load->view('tourismprovider/edit', $data);
    }
    
    /**
     * 删除
     * @param unknown $id
     */
    public function delete($id)
    {
        if (empty($id)) {
            $this->success('tourismprovider/grid', '', 'Id值不能为空');
        }
        $result = $this->tourism_provider->deleteById($id);
        if ($result) {
            $this->success('tourismprovider/grid', '', '删除成功');
        } 
        $this->error('tourismprovider/grid', '', '删除失败');
    }
    
    
    public function editPost()
    {
        $id = $this->input->post('id');
        $postData = $this->input->post();
        $error = $this->validate($postData);
        if (!empty($error)) {
            $this->error('tourismprovider/edit', $id, $error);
        }
        $this->db->trans_start();
        $this->tourism_provider->updateTourismProvider($postData);
        $this->db->trans_complete();
        if ($this->db->trans_status === false) {
            $this->error('tourismprovider/edit', $id, '更新失败！');
        } else {
            $this->success('tourismprovider/grid', '', '更新成功');
        }
    }
    
    /**
     * 上下架
     * @param unknown $id
     * @param unknown $flag
     */
    public function toggle()
    {
        $id = $this->input->post('id');
        $status = $this->input->post('flag');
        switch ($status) {
            case '1': $flag = '2'; break;
            case '2': $flag = '1'; break;
            default : $flag = '2'; break;
        }
        $this->db->trans_start();
        $isUpdate = $this->tourism_provider->updateTourismProviderFlag($flag, $id);
        $this->db->trans_complete();
        
        if ($this->db->trans_status() === TRUE && $isUpdate) {
            echo json_encode(array(
                'flag' => $flag,
            ));
        } else {
            echo json_encode(array(
                'flag' => 3,
            ));
        }
        exit;
    }
    
     /**
     * 检查uid是否存在
     */
    public function checkUidRepeat()
    {
        $uid = $this->input->get('uid');
        $result = $this->tourism_provider->getProviderByUid($uid);
        echo $result->num_rows() > 0 ? 'false' : 'true' ; exit;
    }
    
    /**
     * 分类
     * @param unknown $uid
    */
    public function goods($pg=1)
    {
        $uid = $this->input->get('uid');
        if (empty($uid)) {
            show_404();   
        }
        $page_num = 20;
        $num = ($pg - 1) * $page_num;
        $config['first_url'] = base_url('tourismprovider/goods').$this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('tourismprovider/goods');
        $config['total_rows'] = $this->tourism_goods_base->total($this->input->get());
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_list'] = $this->pagination->create_links();
        $data['line_list'] = $this->tourism_goods_base->page_list($page_num, $num, $this->input->get());
        $data['all_rows'] = $config['total_rows'];
        $data['all_region'] = $this->region->getNewRegion();
        $data['is_check'] = array('1' => '待审核', '2' => '审核通过', '3' => '审核拒绝');
        $data['is_on_sale'] = array('1' => '上架', '2' => '下架');
        $data['pg_now'] = $pg;
        $data['category'] = $this->tourism_provider_category->providerCategoryByUid($uid);
        $data['url'] = $this->pageGetParam($this->input->get());
        $this->load->view('tourismprovider/goods',$data);
    }
    
    
     /**
     * 批量操作分类
     */
    public function batchSetCategory()
    {
        $category_id = $this->input->post('category');
        $goods_id = $this->input->post('goodsId');
        $uid = $this->input->post('uid',true);
        if (empty($category_id)) {
            $this->jsonMessage('供应商产品分类ID不能为空');
        }
        if (count($goods_id)<=0) {
            $this->jsonMessage('产品ID不能为空');
        }
        $param = array(
            'category_id' => $category_id,
            'goods_id' => $goods_id
        );
        $result = $this->tourism_goods_base->updateTourismProviderCategory($param);
        $this->operate_power_log->insertOperatePowerLog(array('uid'=>$this->uid,'name'=>$this->name.'-商品','record'=>'操作类：'.__CLASS__.'-方法：'.__FUNCTION__, 'content'=>json_encode(array('goods_id'=>$goods_id))));
        if ($result) {
            $this->jsonMessage('',base_url('tourismprovider/goods?uid='.$uid));
        }
        $this->jsonMessage('操作失败，请重新操作');
    }
    
     /**
     * validate
     * @param unknown $postData
     */
    public function validate($postData)
    {
        $error = array();
        if( $this->validateParam($postData['uid']) ){
            $error['uid'] = '供应商uid不能为空';
        }
        if( $this->validateParam($postData['name']) ){
            $error['name'] = '店铺名称不能为空';
        }
        if( $this->validateParam($postData['company_name']) ){
            $error['company_name'] = '公司名称不能为空';
        }
        if( $this->validateParam($postData['company_address']) ){
            $error['company_address'] = '公司地址不能为空';
        }
        if( $this->validateParam($postData['main_business'])){
            $error['main_business'] = '主营业务不能为空';
        }
        if( $this->validateParam($postData['detail']) ){
            $error['detail'] = '店铺简介不能为空';
        }
        if( $this->validateParam($postData['sort']) ){
            $error['sort'] = '排序不能为空';
        }
        return $error;
    }
}